﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using tnet.Controls.Lookless;
using tnet.Model;
using Microsoft.Advertising.Mobile.UI;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace tnet
{
    public partial class MyFavorites : PhoneApplicationPage
    {
        private VibrateController _Vibration = VibrateController.Default;
        private TimeSpan _ThisTime = new TimeSpan(0, 0, 0, 0, 30);
        private String TwitterHandle = "DavidJKelley";
        private String _EmailSubjectPrefix = "Transhumanity.net for WP: ";
        private String _BackTileTitle = "Transhumanity.net";

        public MyFavorites()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            AdControl.TestMode = false;
             
            String Temp = Tools.GetSetting("FavoritesList.index").ToString();
            Boolean IsCurrentValue = true;

            if (!(String.IsNullOrEmpty(Temp)))
            {
                FavLstBx.Items.Clear();

                String [] FavoriteKeys = Temp.Split('^');

                for (int x = 0; x < FavoriteKeys.Length; x++)
                {
                    String [] TempNews = Tools.GetSetting(FavoriteKeys[x]).ToString().Split('^');

                    if (TempNews.Length > 1)
                    {
                        RssItem _RssItem = new RssItem();

                        _RssItem.Title = TempNews[0];
                        _RssItem.Link = TempNews[1];
                        _RssItem.Img = TempNews[2];
                        _RssItem.Description = TempNews[3];

                        FavLstBx.Items.Add(_RssItem);
                    }
                    else
                    {
                        IsCurrentValue = false;
                    }
                }

                // clean up...
                if (!IsCurrentValue)
                {  
                    SaveUpdatedFavorites();
                }
            }
        }

        private void NewsRSS_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            try
            {
                ListBox ThisBox = sender as ListBox;
                RssItem ThisItem = ThisBox.SelectedItem as RssItem;

                String FreezeDriedItem = ThisItem.FreezeDried;
                String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

                Tools.SaveSetting("TempNewsItem", FreezeDriedItem);

                NavigationService.Navigate(new Uri("/TempNewsItem.xaml", UriKind.Relative)); 
            } catch(Exception) { }
        }

        private void Home_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _Vibration.Start(_ThisTime);
            NavigationService.GoBack();
        }

        private void LaunchURL(String ThisUrl)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri(ThisUrl);
            webBrowserTask.Show();
        }
         
        private void TheBlazeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            MenuItem ThisItem = sender as MenuItem;
            String FreezeDriedItem = ThisItem.Tag.ToString();
            String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

            // save data...
            String FavStoryKey = Tools.ScrubTitleToKey(FreezeDriedItemArray[0]) + ".fav";
            Tools.SaveSetting(FavStoryKey, FreezeDriedItem);

            // pin news item
            ShellTile SecondTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("/NewsItem.xaml?FD=" + FavStoryKey));

            StandardTileData NewTileData = new StandardTileData();

            NewTileData.Title = FreezeDriedItemArray[0];
            NewTileData.BackgroundImage = new Uri("Resources/News.jpg", UriKind.Relative);
            NewTileData.Count = 0;
            NewTileData.BackTitle = _BackTileTitle;
            NewTileData.BackBackgroundImage = new Uri("Resources/Icon.Daily.jpg", UriKind.Relative);

            if (SecondTile == null)
            {
                ShellTile.Create(new Uri("/NewsItem.xaml?FD=" + FavStoryKey, UriKind.Relative), NewTileData);
            }
            else
            {
                SecondTile.Update(NewTileData);
            } 
        }

        private void EmailNews_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            MenuItem ThisItem = sender as MenuItem;
            String FreezeDriedItem = ThisItem.Tag.ToString();
            String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

            Tools.SendEmail(FreezeDriedItemArray[1], _EmailSubjectPrefix + FreezeDriedItemArray[0]); 
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            MenuItem ThisItem = sender as MenuItem;
            String FreezeDriedItem = ThisItem.Tag.ToString();
            String[] FreezeDriedItemArray = FreezeDriedItem.Split('^');

            // remove item
            for (int x = 0; x < FavLstBx.Items.Count; x++)
            {
                RssItem ThisRssItem = FavLstBx.Items[x] as RssItem;
                if (ThisRssItem.FreezeDried == FreezeDriedItem)
                {
                    FavLstBx.Items.Remove(ThisRssItem);
                    Tools.SaveSetting(Tools.ScrubTitleToKey(ThisRssItem.Title) + ".fav", String.Empty);
                    break;
                } 
            }

            SaveUpdatedFavorites();
        }

        private void SaveUpdatedFavorites()
        {
            String NewFavsList = String.Empty;
            String FavsDelimiter = String.Empty;

            // update items
            for (int z = 0; z < FavLstBx.Items.Count; z++)
            {
                RssItem ThisRssItem = FavLstBx.Items[z] as RssItem;

                NewFavsList += FavsDelimiter + Tools.ScrubTitleToKey(ThisRssItem.Title) + ".fav";

                FavsDelimiter = "^";
            }

            Tools.SaveSetting("FavoritesList.index", NewFavsList);
        }

        private void TwitterBox_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            String[] FreezeDriedItemArray = ReturnFreezeDriedArray(sender);

            Tools.SendTwitter(FreezeDriedItemArray[1], FreezeDriedItemArray[0], TwitterHandle);
        }

        private void FacebookBox_Click(object sender, RoutedEventArgs e)
        {
            _Vibration.Start(_ThisTime);

            String[] FreezeDriedItemArray = ReturnFreezeDriedArray(sender);

            Tools.SendToFacebook(FreezeDriedItemArray[1], FreezeDriedItemArray[0]);
        }

        private String[] ReturnFreezeDriedArray(object sender)
        {
            MenuItem ThisItem = sender as MenuItem;

            String FreezeDriedItem = ThisItem.Tag.ToString();

            return FreezeDriedItem.Split('^');
        }

    }
}